//商品管理功能需要的store
import http from '../../ajax/http'
import * as url from '../../ajax/api'
import axios from 'axios'
const state = {
    goodsData:JSON.parse(localStorage.getItem('goodsData')) || []
}
const actions = {
    getGoodsData({commit}){
        console.log('我执行了')
        return new Promise((resolve,reject)=> {
            axios({
                url:url.getGoodsData,
                method:'get'
            }).then(res=>{
                console.log(res)
                if(res.status == 200){
                    commit('setGoodsData',res.data)
                    resolve();
                }    
            })     
        })     
    },
    deleteGoodsData({commit},id){
        return new Promise((resolve,reject)=> {
            axios({
                url:url.deleteGoodsData,
                method:'post',
                data:{id}
            }).then(res=>{
                if(res.status == 200){
                    actions.getGoodsData({commit})
                resolve();
                }  
            })     
        })     
    },
    editGoodsData({commit},obj){
        return new Promise((resolve,reject)=> {
            axios({
                url:url.editGoodsData,
                method:'post',
                data:obj
            }).then(res=>{
                if(res.status == 200){
                    actions.getGoodsData({commit})
                resolve();
                }  
            })     
        })    
    }
}
const mutations = {
    setGoodsData(state,data){
        localStorage.setItem('goodsData',JSON.stringify(data));
        state.goodsData = data;
    }
}
const getters = {

}
export default{
    state,
    actions,
    mutations,
    getters,
}